#ifndef __APP_H__
#define __APP_H__
#include <semaphore.h>
#include "interaction.h"
#include "communication.h"
#include <gtk/gtk.h>

#define CLEAR()         printf("\033[2J")           /*清除屏幕*/
#define MOVEUP(x)       printf("\033[%dA", (x))     /*上移光标*/

#define MAX_SELECT_NUM  100
#define MAX_WORK_THREAD_NUM  5

/* 函数指针的个数 */
#define OPTION_NUM (12)

/* 线程同步 */
sem_t sem;
sem_t sem_find_ticket;
sem_t sem_ack;
sem_t sem_ticket;
sem_t sem_user;
sem_t sem_destroy1;
sem_t sem_user_ticket;
sem_t sem_run;

/*所有线程定义在这里*/
struct server_pthreads {
    pthread_t recv_thread;/*接收信息的线程*/
    pthread_t work_thread[MAX_WORK_THREAD_NUM];/*工作线程*/
    pthread_t destroy_thread;
    pthread_t gtk_thread;
};

/*所有缓冲区定义在这个结构体*/
struct ring_buffers {
    struct ring_buffer *to_recv_pthread_fd;/*用于给select线程传fd的buf*/
    struct ring_buffer *recv_data;/*系统接收需要处理的信息的buf*/
};

struct heart {
    /* 上一次的时间 */
    time_t last_heart_time[10];
    /* 对应的fd */
    int heart_fd[10];
    /*有无数据*/
    int is_data[10];
};

struct app {
    struct interaction *interaction;
    struct ring_buffers ring_buffers;/*程序所用到的buffer*/
    struct server_pthreads server_pthreads;/*所有线程*/
    struct heart heart_bag;
};

/*写入recv_data的结构体，就是在通信收到的结构体上加入对应用户的fd*/
struct data_with_fd {
    int clientfd;
    struct data_packet data;
};

extern func_t *init(func_t *p_func);
extern int run(void);
extern void destroy(void);

#endif